package com.paas.form.core.element.control.impl.searchItem;

import com.paas.common.util.StringUtil;
import com.paas.form.core.element.base.Utilmethod;
import com.paas.form.core.element.control.FormItem;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.jsContent.ScriptContent;
import lombok.Data;

@Data
public class FormSelectItem implements FormItem {
    private FieLdsModel field;

    private String htmlType;

//    public static final String EL_SELECT_JS_DATA = "                ${field.vModel}Options :  " +
//            "[\n #foreach($item in ${field.schemeSlot.options})\n" +
//                "#set($maohao=':')\n" +
//                "                  {" +
//                "#if(${item.id})\n" +
//                    "id${maohao}'${item.id}'," +
//                "#end\n" +
//                "#if(${item.fullName})\n" +
//                    "fullName${maohao}'${item.fullName}'," +
//                "#end\n" +
//                "#if(${item.parentId})\n" +
//                    "parentId${maohao}'${item.parentId}'," +
//                "#end\n" +
//                "#if(${item.hasChildren})\n" +
//                    "hasChildren${maohao}'${item.hasChildren}'," +
//                "#end\n" +
//                "#if(${item.children})\n" +
//                    "children${maohao}'${item.children}'," +
//                "#end\n" +
//                "#if(${item.children})\n" +
//                    "children${maohao}'${item.children}'," +
//                "#end\n" +
//                "#if(${item.enCode})\n" +
//                    "enCode${maohao}'${item.enCode}'," +
//                "#end\n" +
//                "},\n" +
//            "#end " +
//            "               ],\n";
    public static final String EL_SELECT_JS_DATA_OLD = "${field.vModel}Options : ${field.schemeSlot.options}";

    public static final String EL_SELECT_JS_DATA = "                ${field.vModel}Options : JSON.parse('${field.schemeSlot.options}'),";


    public static final String EL_SELECT_HTML ="<el-select v-model=\"${formObject}.${field.vModel}\" placeholder=\"${field.placeholder}\"\n" +
            "                                                           clearable #if(${field.multiple} =='true') multiple #end  #if(${field.filterable} =='true') filterable #end> \n " +
            "                                                    <el-option v-for=\"(item, index) in ${field.vModel}Options\" :key=\"index\"\n" +
            "                                                               :label=\"item.${field.config.Props.label}\" :value=\"item.${field.config.Props.value}\"\n" +
            "                                                               :disabled=\"item.disabled\"></el-option>\n" +
            "                                                </el-select>";


    public String getelSelectHtml(){
        String elSelectHtml= "<el-select v-model=\"${formObject}.${field.vModel}\" placeholder=\"${field.placeholder}\"\n";
        if(this.htmlType.equals("form")){
            if(field.getDisabled() != null && field.getDisabled().equals(true)){
                elSelectHtml = elSelectHtml +" :disabled=\"true\" ";
            }else{
                elSelectHtml = elSelectHtml +" :disabled=\"isdetail\" ";
            }
        }
        elSelectHtml = elSelectHtml +
                "                                                           clearable #if(${field.multiple} =='true') multiple #end  #if(${field.filterable} =='true') filterable #end> \n " +
                "                                                    <el-option v-for=\"(item, index) in ${field.vModel}Options\" :key=\"index\"\n" +
                "                                                               :label=\"item.${field.config.Props.label}\" :value=\"item.${field.config.Props.value}\"\n" +
                "                                                               :disabled=\"item.disabled\"></el-option>\n" +
                "                                                </el-select>";
        return elSelectHtml;
    }

    public static String getCheckElement(FieLdsModel field) {
        String optionTypeStr = "";
        if(field.getConfig() != null && field.getConfig().getOptionType() != null && field.getConfig().getOptionType().equals("button")){
            optionTypeStr = "-button ";
        }
        String idstr= "";
        if(field.getConfig() != null && field.getConfig().getProps() != null &&field.getConfig().getProps().getValue() != null){
            idstr = field.getConfig().getProps().getValue();
        }
        String namestr= "";
        if(field.getConfig() != null && field.getConfig().getProps() != null &&field.getConfig().getProps().getLabel() != null){
            namestr = field.getConfig().getProps().getLabel();
        }
        String borderstr= "";
        if(field.getConfig() != null && field.getConfig().getBorder() != null &&field.getConfig().getBorder().equals(true)){
            borderstr = "border ";
        }
        //属性项
        String attrstr= "";
        if(StringUtil.isNotEmpty(field.getSize())){
            attrstr= attrstr +" size=\"" +field.getSize() + "\" ";
        }
        if(field.getDisabled() != null && field.getDisabled().equals(true)){
            String attrItem= " :disabled=\"" +field.getDisabled() + "\" ";
            attrstr = attrstr + attrItem;
        }else{
            String attrItem= " :disabled=\"isdetail\" ";
            attrstr = attrstr + attrItem;
        }



        String elementStart ="<el-" +field.getConfig().getPfKey()+"-group"+" v-model=\"${formObject}.${field.vModel}\"  " +attrstr + " >\n" +
                "\t<el-" +field.getConfig().getPfKey() +optionTypeStr + " v-for=\"(item, index) in " +field.getVModel() +"Options\" :key=\"index\" :label=\"item."
                + idstr +"\" " +borderstr +">{{item." +namestr +"}}" ;
        String elementEnd = "</el-" +field.getConfig().getPfKey() +optionTypeStr +" >\n" +
                "</el-" +field.getConfig().getPfKey()+"-group" +" >\n";
        String element= elementStart +elementEnd;
        return element;
    }
    public FormSelectItem (FieLdsModel field,String htmlType){
        this.field = field;
        this.htmlType = htmlType;

    }


    @Override
    public String makeVue() {
        if(this.htmlType.equals("form")){
            switch (this.field.getConfig().getPfKey()){
                //useSelectList['radio', 'checkbox', 'select']
//                case "radio":
//                    return EL_SELECT_HTML;
//                case "checkbox":
//                    return EL_SELECT_HTML;
                case "select":
                    return getelSelectHtml();
                default:
                    return getCheckElement(this.field);

            }
        }else {
            return EL_SELECT_HTML;
        }




    }

    @Override
    public ScriptContent makeJs() {
        ScriptContent scriptContent = new ScriptContent();
        scriptContent.setData(EL_SELECT_JS_DATA);
        return scriptContent;
    }
}
